package com.hzdesk.media.koreabatchmatvector.adapter;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.bean.WifiBean;
import com.hzdesk.media.koreabatchmatvector.config.AppContants;
import com.hzdesk.media.koreabatchmatvector.inter.OnclikedWiFiSetting;

import java.util.List;

/**
 * Created by Administrator on 2018/7/30.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class WiFiSettingAdapter extends RecyclerView.Adapter<WiFiSettingAdapter.ViewHolder> {

    private List<WifiBean> mlist;

    public void setmOnclikedWiFiSetting(OnclikedWiFiSetting mOnclikedWiFiSetting) {
        this.mOnclikedWiFiSetting = mOnclikedWiFiSetting;
    }

    private OnclikedWiFiSetting mOnclikedWiFiSetting = null;
    public WiFiSettingAdapter(Context mContext) {
    }

    @Override
    public WiFiSettingAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.wifi_setting_item,parent,false);
        ViewHolder mViewHolder = new ViewHolder(view);
        return mViewHolder;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindViewHolder(WiFiSettingAdapter.ViewHolder holder, final int position) {
        holder.tv_desc.setText(mlist.get(position).getState()+"");
        holder.tv_wifi_name.setText(mlist.get(position).getWifiName());
        String state = mlist.get(position).getState();
        if(state.equals(AppContants.WIFI_STATE_CONNECT)){
            holder.iv_wifi.setBackgroundResource(R.drawable.network);
        }else{
            holder.iv_wifi.setBackgroundResource(R.drawable.no_network);
        }
        holder.tv_desc.setText(state);
        holder.rl_onclicked.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnclikedWiFiSetting != null){
                    mOnclikedWiFiSetting.onClickedItem(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mlist==null?0:mlist.size();
    }


    public void  AddItem(List<WifiBean> mScanResults){
        mlist=mScanResults;
        notifyDataSetChanged();
    }
     class ViewHolder extends RecyclerView.ViewHolder {

         private final ImageView iv_wifi;
         private final TextView tv_wifi_name;
         private final TextView tv_desc;
         private final RelativeLayout rl_onclicked;

         public ViewHolder(View itemView) {
             super(itemView);
             iv_wifi = itemView.findViewById(R.id.wifi_setting_item_iv_wifi);
             tv_wifi_name = itemView.findViewById(R.id.wifi_setting_item_tv_wifi_name);
             tv_desc = itemView.findViewById(R.id.wifi_setting_item_tv_desc);
             rl_onclicked = itemView.findViewById(R.id.wifi_setting_item_rl_onclicked);
         }
     }
}
